<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class product_size extends CI_Controller{
	
	public function search(){
		if($this->check_session()){
			$this->load->Model("product_size_DAO");
			$keyword = $_POST['keyword'];
			$data['product_size_list'] = $this->product_size_DAO->search($keyword);
			$this->parser->parse('table/productsize', $data);
		} else {
			$this->logout();
		}
    }
	
	public function get_list(){
		if($this->check_session()){
			$category_level_2_id = $_POST['category_level_2_id'];
			$type_of_size_id = $_POST['type_of_size_id'];
			$this->load->Model("product_size_DAO");
			$data['product_size_list'] = $this->product_size_DAO->getAll($category_level_2_id, $type_of_size_id);
			$this->parser->parse('option/product_size', $data);
		} else {
			$this->logout();
		}
    }
	
	public function get_info(){
		if($this->check_session()){
			$this->load->Model("product_size_DAO");
			$id = $_POST['id'];
			$returnData = $this->product_size_DAO->get($id);
			if($returnData == null){
				echo $returnData;
			} else {
				echo json_encode($returnData);
			}
		} else {
			$this->logout();
		}
    }
	
	public function add(){
		if($this->check_session()){
			$this->load->Model("product_size_DAO");
			$this->load->Model("model/product_size_model");
			$category = new product_size_model();
			$category->category_level_2_id = $_POST['category_level_2_id'];
			$category->type_of_size_id = $_POST['type_of_size_id'];
			$category->size_name = $_POST['size_name'];
			echo $this->product_size_DAO->add($category);
		} else {
			$this->logout();
		}
    }
	
	public function update(){
		if($this->check_session()){
			$this->load->Model("product_size_DAO");
			echo $this->product_size_DAO->update($_POST['id'], $_POST['category_level_2_id'], $_POST['type_of_size_id'], $_POST['size_name']);
		} else {
			$this->logout();
		}
	}
	
	public function delete(){
		if($this->check_session()){
			$this->load->Model("product_size_DAO");
			echo $this->product_size_DAO->delete($_POST['id']);
		} else {
			$this->logout();
		}
	}
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
	
	public function check_session(){
		$session_username = $this->session->userdata('session_username');
		return isset($session_username) && $session_username != '';
	}
}